/*
 *	main.cpp is part of Flight_Chess_Portable
 *	Copyright (C) 2008  Poison
 *
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 *	Class:			main
 *	Description:	
 *	Author:			Poison <hbpoison@gmail.com>
 *	Date Created:	2008-03-20
 */

#include "main.h"

#include <pspsdk.h>
#include <pspuser.h>
#include <pspdisplay.h>

PSP_MODULE_INFO("fcp", 0, 1, 0);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER);

MainApp * main_app;

int ExitCallback::reg()
{
	if( valid() )
	{
		return sceKernelRegisterExitCallback( getId() );
	}
	return -1;
}

int ExitCallback::CallbackFunction( int arg1, int arg2, void * arg )
{
	sceKernelExitGame();
	return 0;
}

int CallbackThread::ThreadEntry( int argc, void * argp )
{
	exit_cb = new ExitCallback( "exit_callback" );
	exit_cb->reg();
	Thread::sleepCB();
	return 0;
}

int MainThread::ThreadEntry( int argc, void * argp )
{
	pspDebugScreenInit();
	pspDebugScreenPrintf( "work!" );
	return 0;
}

int MainApp::init( int args, void * argp )
{
	threads = new Thread*[2];
	threads[0] = new CallbackThread( false, "callback_thread" );
	threads[1] = new MainThread( true, "main_thread" );
	thread_count = 2;
	return 0;
}

#if _PRX_MODE == 1
extern "C" {
int module_stop( int args, void * argp )
{
	delete main_app;
	return 0;
}

int module_start( int args, void * argp )
#else
int main( SceSize args, void *argp )
#endif
{
	main_app = new MainApp();
	main_app->start( args, argp );
	Thread::sleep();
	return 0;
}
#if _PRX_MODE == 1
}
#endif
